<?php
class University extends CI_Controller 
{
	public function __construct(){
		parent::__construct();
	} 
	//获得 省链接
	public function get_province(){
		die();//已经写入成功了
		$url = 'http://ziyuan.eol.cn/college.php';
		$html = file_get_contents($url);
		$pattern = '/<a[^href]+?href="(l[^"]+?)">([^<]+?)<\/a>/i';		
		preg_match_all($pattern, $html, $mat);
//		print_r($mat);
		if(preg_match_all($pattern, $html, $mat) != FALSE){
			$prov_urls = $prov = array();
			$prov_urls = $mat[1];
			$prov = $mat[2];
			foreach ($prov as $k=>$v){
				$this->db->insert('univer_province', array('province'=>$v, 'provinceurl'=>"http://ziyuan.eol.cn/".$prov_urls[$k]));
				echo $v, '写入成功<br />';
			}
		}		
	}
	//根据省份链接，得到大学列表
	public function get_college(){
		die();//已经成功执行
		//test first
//		$pr = '北京'; $pru = 'http://ziyuan.eol.cnlist.php?listid=151';
		$pr = '西藏'; $pru = 'http://ziyuan.eol.cn/list.php?listid=630';
		$html = file_get_contents($pru);
		$pattern = '/<a class=a17 [^>]+?>([^<]+?)<\/a>/i';
		if(preg_match_all($pattern, $html, $mat) != FALSE){
			print_r($mat);
		}
		
		$pattern = '/<a class=a17 [^>]+?>([^<]+?)<\/a>/i';
		$pros = $this->db->query("SELECT province, provinceurl FROM univer_province");
		foreach ($pros->result() as $row){
			$pr = $row->province;
			$pru = $row->provinceurl;
			$html = file_get_contents($pru);
			if(preg_match_all($pattern, $html, $mat) != FALSE){
				foreach ($mat[1] as $u){
					$setarr = array('name'=>$u, 'province'=>$pr);
					echo '开始写入', $u, '...<br />';
					if($this->db->insert('university', $setarr)){
						echo '.ok.!<br />';
					}
					
				}
			}
		}
		
	}
	
	
	public function pregtest(){
		$url = 'http://ziyuan.eol.cn/college.php';
		preg_match('/^(http:\/\/)?([^\/])+/',$url, $mat);
		
		$str='Catwoman';
		preg_match_all('/\s+^bat/i',$str,$mat);
		print_r($mat);
		
		$time=date("Y-m-d H:i:s");
		$pattern = "/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/i";
		$replacement = "\$time格式为：$0<BR>替换后的格式为：$1年$2月$3日 $4时$5分$6秒";
		print preg_replace($pattern, $replacement, $time);
		
		preg_match("/\d{4}-\d{2}-\d{2} (\d{2})/i", $time, $mat);
		print_r($mat);
		$str='<a class=a13 href="list.php?listid=623">内蒙古</a> 
		<a class=a13 href="list.php?listid=153">河北</a>';
		preg_match_all('/<a[^href]+?href=\"([^\"]+?)\">([^<]+?)(?=<\/a>)/i', $str, $mat);
		print_r($mat);
	}
	
}
/* End of file university.php */
/* Location: ./application/controller/university.php */